<html>
<head>	<title>Chapter 7 Solutions to Programming Exercises </title>	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">	<link rel="Stylesheet" type="text/css" title="phStyle" href="../../../html/css/style.css">	<link rel="icon" href="../../../phicon.ico" type="image/bmp"></head><body bgcolor="#ffffff"><div align="center"><table width="80%" border="0" cellspacing="0" cellpadding="0">	<tr>	<td valign=top>	<span class="header">Chapter 7 Solutions to Programming Exercises </span><br>
 
<p>
Chapter 7 says it is about writing a video game, but the real
topics are multi-threading, events, and shared data.
</p>

<p>
The exercises in the chapter provide practice in working with 
the system calls and style for signals, curses, and some interprocess
communication.
</p>
<dl>
<dt>Solution 7.8
<dd>
	<p>
	A simple application of curses and sleep to produce a
	blinking message is
	<a href='sol07.8.c'><tt>sol07.8.c</tt></a>.
	Many terminals/emulators support a blinking attribute,
	but this program produces blinking with software not
	with (virtual) hardware.  Does anyone actually use a
	terminal anymore?
	</p>
	</dd>
<dt>Solution 7.9
<dd>
	<p>
	A ticker-tape/marquee display viewer for files is
	<a href='sol07.9.c'><tt>sol07.9.c</tt></a>.
	This program is much more interesting than problem 7.8.
	The solution here uses an object-oriented approach by
	creating a ticker-tape-display object and feeding characters
	into that object.  That solution is good preparation for
	some pipe/socket and client/server oriented solutions 
	later in the book.
	</p>
	</dd>
<dt>Solution 7.10
<dd>
	<p>
	A version of hello5.c that uses usleep() and has varying
	speed is
	<a href='sol07.10.c'><tt>sol07.10.c</tt></a>.
	This version simulates simple harmonic motion by varying
	the speed according to the sine function.  The timing
	is isolated in a function called delay().  That function
	could be replaced by one that returns a delay based on
	the acceleration of gravity. Or any other function for
	that matter.
	</p>
	</dd>
<dt>Solution 7.11
<dd>
	<p>
	A version of ticker_demo.c that uses a global variable
	so the signal handler can inform the main function when
	the program should exit is
	<a href='sol07.11.c'><tt>sol07.11.c</tt></a>.
	This change is minor, but the technique is useful.
	The advantage of telling main when to exit is that
	main can call any wrap-up functions.  It is cleaner
	design to exit from main.  The disadvantage is that
	global variables are risky for the usual reasons.
	</p>
	</dd>
<dt>Solution 7.12
<dd>
	<p>
	A version of sigdemo3.c that uses one handler for
	both SIGINT and SIGQUIT is
	<a href='sol07.12.c'><tt>sol07.12.c</tt></a>.
	This program shows how one handler can be used for
	more than one type of signal.  The handler contains
	a line of common code and two lines of signal-specific
	code.
	</p>

<p>
	This program raises the question of how a single
	handler for two different signals affects the arrival
	of SIGX while the program is handling SIGY.  The
	handler is the same, but the signals are different.
	Can an instance of SIGQUIT interrupt the handling
	of SIGINT, even if the handler is the same?  That is,
	does the handler or the signal determine if a handler
	is interrupted.  On Linux 2.2.14, the single handler
	version operates the same way the version with two
	different handlers does.
	</p>
	</dd>
<dt>Solution 7.13
<dd>
	<p>
	A program for autologout is
	<a href='sol07.13.c'><tt>sol07.13.c</tt></a>.
	This program uses sleep and kill.  If a filename argument
	is given, the program waits until that file has not been
	modified for the specified number of seconds before sending
	SIGKILL to the specified process.  That second feature 
	requires using stat() to find the modification time of
	the file and computing the sleep interval based on that
	time and the current time.  
	</p>

<p>
	The program could be done with a polling loop that kept
	checking the modification time of the file, but that would
	waste CPU time.
	</p>

<p>
	The program could be called with the command line:
	<pre>timeout $$ 3600 `tty` &amp;</pre>
	to pass it the process id of the current shell and the
	name of the terminal file for the current shell.
	</p>

<p>
	By allowing any file name as the last argument, the program
	is more flexible than a simple autologout tool.  If the
	signal were other than SIGKILL, the program could be used
	to notify a process when a file was no longer changing.
	</p>
</dd>
<dt>Solution 7.14
<dd>
	<p>
	A program that prints a message after receiving a certain
	number of SIGINTs is
	<a href='sol07.14a.c'><tt>sol07.14a.c</tt></a>.
	and the driver program that sends SIGNTs to a list of
	processes at regular intervals is
	<a href='sol07.14b.c'><tt>sol07.14b.c</tt></a>.
	This exercise provides practice writing programs that
	send signals, programs that receive signals, and simple
	timers.  The exercise also provides some practice creating
	and managing background processes.  Finally, the exercise
	sets some groundwork for the next chapter on processes.
	</p>
</dd>
<dt>Solution 7.15
<dd>
	<p>
	A program that blocks on time and handles keyboard events
	as signals is
	<a href='sol07.15.c'><tt>sol07.15.c</tt></a>.
	This program uses async input handling to reverse the
	design of the original bounce1d program.  In the original
	program, the program blocked on user input and 
	timer events were handled to drive the animation.
	This version provides some practice in using user input
	as events that can be handled.  This version also provides
	more practice in selecting and comparing design models
	for a system.  Which of the three versions is better or
	easier, or more efficient?  The one with two handlers, the
	one with a timer handler, the one with a keyboard handler?
	</p>
</dd>
<dt>Solution 7.16
<dd>
	<p>
	A version of a reaction time tester is
	<a href='sol07.16.c'><tt>sol07.16.c</tt></a>.
	This program uses curses and requires using
	gettimeofday to obtain the current time at higher
	resolution than time(2) provides.
	</p>
	<p>
	The design of a user interface is important in this
	program. The interface affects the results.  The program
	contains ideas that apply to any program that needs to
	measure times or intervals of input from external devices.
	</p>
</dd>
<dt>Solution 7.17
<dd>
	<p>
	There is no simple solution to the pong game.  This
	exercise is a complete project.  
	</p>
</dd>
</dl>
</td></tr></table></div><br clear="all"><table border=0 align=right>	<tr>	<td class="footer">	&copy; 2003 <a href="http://www.prenhall.com" target="new">Pearson Education, Inc.</a><br>	<a href="../../../html/notice/legal.html" target="main">Legal Notice</a><br>			<a href="../../../html/notice/privacy.html" target="main">Privacy Statement</a><br>			<a href="../../../html/tech_support.html" target="main">Technical Support Info</a>	</td></tr></table><BR CLEAR="all"></body></html>